package servlet;

import dao.AdministratorDao;
import dao.ArticleDao;
import dao.CommentDao;
import dao.daoimpl.AdministratorImpl;
import dao.daoimpl.ArticleDaoImpl;
import dao.daoimpl.CommentDaoImpl;
import bean.Article;
import bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name="ManagementServlet",urlPatterns = "/ManagementServlet")
public class ManagementServlet extends HttpServlet{
    private AdministratorDao administratorDao=new AdministratorImpl();
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");/*防止乱码*/
        String action=request.getParameter("action");
        if(action.equals("deleteBlog")){
            try {
                deleteBlog(request,response);
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }else if(action.equals("updateBlog")){
            try {
                updateBlog(request,response);
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }else if(action.equals("commandBlog")){
            try {
                commandBlog(request,response);
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }else if(action.equals("deleteUser")){
            try {
                deleteUser(request,response);
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }else if(action.equals("updateUser")){
            try {
                updateUser(request,response);
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }else if(action.equals("browseBlog")){
            try {
                browseBlog(request,response);
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        }else if(action.equals("deleteCommand")){

            try {
                deleteCommand(request,response);
            } catch (SQLException sqlException) {
                sqlException.printStackTrace();
            }
        } else if(action.equals("checkBlog")){
            try {
                checkBlog(request,response);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }else{
            System.out.println("操作错误");
        }
    }
    private void deleteBlog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        int deleteAccount=Integer.parseInt(request.getParameter("deleteAccount"));
        if(administratorDao.deleteBlog(deleteAccount)) {
            System.out.println("管理员删除博客成功");
            request.getSession().setAttribute("administratorArticles",administratorDao.getAllArticles());
        }else {
            System.out.println("管理员删除博客失败");
        }
    }
    private void updateBlog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        int account=Integer.parseInt(request.getParameter("updateAccount"));
        String time=request.getParameter("updateTime");
        String type=request.getParameter("updateType");
        String title=request.getParameter("updateTitle");
        String subfield=request.getParameter("updateSubfield");
        String user=request.getParameter("updateUser");
        int category=Integer.parseInt(request.getParameter("updateCategory"));
        int commentCount= Integer.parseInt(request.getParameter("updateCommentCount"));
        int views=Integer.parseInt(request.getParameter("updateViews"));
        int likingDegree=Integer.parseInt(request.getParameter("updateLikingDegree"));
        int like=Integer.parseInt(request.getParameter("updateLike"));
        if(administratorDao.updateBlog(new Article(account,likingDegree,views,title,time,commentCount,category,user,type,subfield,like))){
            System.out.println("管理员修改博客成功");
            request.getSession().setAttribute("administratorArticles",administratorDao.getAllArticles());
        }else{
            System.out.println("管理员修改博客失败");
        }

    }
    private void commandBlog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        int commandAccount=Integer.parseInt(request.getParameter("commandAccount"));
        if(administratorDao.commandBlog(commandAccount)){
            request.getSession().setAttribute("administratorCommands",administratorDao.getAllCommand());
            System.out.println("管理员推荐博客成功");
        }else{
            System.out.println("管理员推荐博客失败");
        }
    }
    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        String deleteUser=request.getParameter("deleteAccount");
        if(administratorDao.deleteUser(deleteUser)){
            System.out.println("管理员删除用户成功");
            request.getSession().setAttribute("administratorUsers",administratorDao.getAllUsers());
        }else{
            System.out.println("管理员删除用户失败");
        }
    }
    private void updateUser(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        User user=new User();
        user.setPsw(request.getParameter("updatePsw"));
        user.setAccount(request.getParameter("updateAccount"));
        user.setScreenName(request.getParameter("updateScreenName"));
        user.setName(request.getParameter("updateName"));
        user.setPhone(request.getParameter("updatePhone"));
        user.setEmail(request.getParameter(request.getParameter("updateEmail")));
        user.setSign(request.getParameter("updatePhone"));
        user.setSex(request.getParameter("updateBirthday"));
        user.setAddress(request.getParameter("updateAddress"));
        user.setBirthday(request.getParameter("updateBirthday"));
        user.setAge(Integer.parseInt(request.getParameter("updateAge")));
        if(administratorDao.updateUser(user)){
            System.out.println("管理员修改用户成功");
            request.getSession().setAttribute("administratorUsers",administratorDao.getAllUsers());
        }else{
            System.out.println("管理员修改用户失败");
        }
    }
    private void browseBlog(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        int articleId=Integer.parseInt(request.getParameter("browseAccount"));
        ArticleDao articleDao=new ArticleDaoImpl();
        CommentDao commentDao=new CommentDaoImpl();
        try {
            request.getSession().setAttribute("blogArticle",articleDao.getArticle(articleId));/*返回数据*/
            request.getSession().setAttribute("blogComments",commentDao.getComments(articleId));
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        System.out.println("管理员查看博客失败");
    }
    private void deleteCommand(HttpServletRequest request,HttpServletResponse httpServletResponse) throws SQLException {
        int articleId=Integer.parseInt(request.getParameter("deleteAccount"));
        if(administratorDao.deleteCommand(articleId)){
            System.out.println("管理员取消推荐成功");
            request.getSession().setAttribute("administratorCommands",administratorDao.getAllCommand());
        }else{
            System.out.println("管理员取消推荐失败");
        }
    }
    private void checkBlog(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        int articleId=Integer.parseInt(request.getParameter("checkAccount"));
        if(administratorDao.checkBlog(articleId)){
            System.out.println("管理员审核博客成功");
            request.getSession().setAttribute("administratorArticles",administratorDao.getAllArticles());
        }else{
            System.out.println("管理员审核博客失败");
        }
    }
}
